// 关于vuex的显示讲解 https://juejin.im/post/5d2d64f36fb9a07eba2c6f65

import Vue from 'vue'
import Vuex from 'vuex'
import types from './mutation-types'
import defaultMenu from '../services/default'
// import * as api from '../api'
// import { getCurrentMenu, getSessionKey } from '../common/utils'

Vue.use(Vuex)

const store = new Vuex.Store({
// process.env.NODE_ENV !== 'production', 直接修改state 抛出异常
  strict: true,
  state: {
    loading: false,
    menuList: [],
    sideBar: {
      opened: true
    },
    device: {
      isMobile: false
    },
    userInfo: { name: '佚名' }
  },
  getters: {
    loading: state => state.loading,
    menuList: state => state.menuList,
    sideBar: state => state.sideBar,
    userInfo: state => state.userInfo
  },
  mutations: {
    // 同步的函数
    [types.TOGGLE_DEVICE] (state, isMobile) {
      state.device.isMobile = isMobile
    },
    [types.TOGGLE_LODING] (state) {
      state.loading = !state.loading
    },
    [types.SET_USER_INFO] (state, info) {
      state.userInfo = info
    },
    [types.LOAD_MENU] (state, menu) {
      state.menuList = menu
    },
    [types.TOGGLE_SIDEBAR] (state, open) {
      if (open === null) {
        open = !state.sideBar.opened
      } else {
        state.sideBar.opened = open
      }
    }
  },
  actions: {
  // 异步的函数
    toggleLoading: ({commit}) => commit(types.TOGGLE_LODING),
    loadMenuList: ({commit}) => {
      Vue.axios.get('./static/data/data.json').then(res => {
        commit(types.LOAD_MENU, res.data.menuList)
      }).catch(exp => commit(types.LOAD_MENU), defaultMenu)
    }
  }
})
export default store
